﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Write.Api.Application.Command.CommodityCommand;

namespace Traceability.Write.Api.Application.CommandHander.CommodityHander
{
    public class UpdateCommodityHander : IRequestHandler<UpdateCommodityCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Commodity> baseRequsitory;

        public UpdateCommodityHander(IMapper mapper, IBaseRequsitory<Commodity> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 修改商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateCommodityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var entity = mapper.Map<Commodity>(request);
            entity.UpdateTime = DateTime.Now;
            await baseRequsitory.UpdateAsync(entity);

            result.Code = ApiEnum.修改成功;
            result.Msg = ApiEnum.修改成功.ToString();

            return await Task.FromResult(result);
        }
    }
}
